package com.chilly.other.jdk8.Interface;

import org.junit.Test;

//定义函数式接口
@FunctionalInterface
interface Supermarket {
    void buy();
}

/**
 * @project: jdk-learnig
 * @description: 通过this引用成员方法
 * @author: 黄宇辉
 * @date: 8/18/2019-4:30 PM
 * @version: 1.0
 * @website: https://yubuntu0109.github.io/
 */
public class RefMethodByThis {

    public void buyLaptop() {
        System.out.println("buy a laptop successfully");
    }

    public void printResult(Supermarket supermarket) {
        supermarket.buy();
    }

    //传统写法
    @Test
    public void testPrintResult() {
        printResult(new Supermarket() {
            @Override
            public void buy() {
                buyLaptop();
            }
        });
    }

    //Lambda表达式写法
    @Test
    public void testPrintResultWithLambda() {
        printResult(() -> this.buyLaptop());
    }

    //方法引用写法
    @Test
    public void testPrintResultWithMethodRef() {
        printResult(this::buyLaptop);
    }

}